<template>
  <div class='m-notice'>
    <el-form :inline='true' style='padding-bottom: 20px'>
      <el-button type='primary' @click='toCreateNotice'>创建公告</el-button>
    </el-form>
    <a-table :data='data' :cols='tableKeys'>
      <el-table-column align="center" label="操作" width='180' slot='ahead'>
        <template slot-scope="scope">
          <div>
            <el-button size='mini' @click='choose(scope.row.id)'>详情</el-button>
            <el-button size='mini' @click='deleteNotice(scope.row.id)'>删除</el-button>
          </div>
        </template>
      </el-table-column>
    </a-table>
    <div class='u-pagination'>
      <!-- 分页 -->
      <pagination ref='page' @change='getData' :action='fetchData' :pageSize='20'></pagination>
    </div>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'

export default {
  name: 'm-notice',

  data() {
    const filterTime = (value) => {
      const d = new Date(parseInt('' + value + '000', 10))
      return window.$tools.formatDate(d)
    }
    // const filterOrderStatus = window.$tools.filterStatus
    return {
      tableKeys: [
        { prop: 'title', label: '公告标题', minWidth: '100' },
        { prop: 'releaser_name', label: '创建人', minWidth: '100' },
        { prop: 'release_time', label: '创建时间', minWidth: '100', filter: filterTime }
      ],
      data: [],
      ifRequesting: false,
      status: '',
      statusList: [
        { value: '0', label: '所有消息' },
        { value: '', label: '未读消息' },
        { value: '1', label: '已读消息' }
      ]
    }
  },

  mounted() {
    this.searchFun()
  },

  methods: {
    ...mapMutations(['setTabRoutes']),

    routerPush(type, args) {
      // push 到缓存里
      this.setTabRoutes({ type: 'add', obj: { ...this.getRouterInfoAccordingName(type), ...args } })
    },

    async fetchData(aPageNum, aPageSize) {
      return this.$api.linkGetNotices({
        aMerchantId: this.aFirmId,
        aPageNum,
        aPageSize
      })
    },

    searchFun() {
      this.$refs.page.changePage()
    },

    getData(data) {
      this.data = data
    },

    choose(id) {
      const query = { type: 'set', id }
      this.routerPush('message_update', { query })
      this.$router.push({ name: 'message_update', query })
    },

    toCreateNotice() {
      this.routerPush('message_add')
      this.$router.push({ name: 'message_add' })
    },

    deleteNotice(id) {
      if (this.ifRequesting) return false
      this.__messageBox({
        title: '你确认要删除该公告吗？',
        successCallback: async () => {
          this.ifRequesting = true
          const { code } = await this.$api.linkDeleteNotice({ aId: id })
          this.ifRequesting = false
          this.$refs.page.changePage()
          return code
        }
      })
    }
  }
}
</script>

<style lang='less'>

</style>